// pagesMine/pages/businessCardEdit/businessCardEdit.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {},
    positionArrIndex: 0,
    postArr: ['法人','股东','高管','人事','行政','普通员工'],
    postIndex: 0,
  },
  /**
       * 事件- 职位选择
       */
  postArrChange: function (e) {
    let that = this
    this.setData({
      postIndex: parseInt(e.detail.value),
      ['form.post']: that.data.postArr[parseInt(e.detail.value)]
    })
  },

  /**
   * 接口 - 获取名片详情
   */
  get_card_detail: function (id) {
    let that = this
    ajax.get(`/wx/appletCard/getInfo?id=${id}`).then(res => {
      if (res.code == 200) {
        let data = res.data
        let form = {}
        form.id = data.id
        form.checkCardId = data.checkCardId
        form.companyProfile = data.companyProfile
        form.mailbox = data.mailbox
        form.name = data.name
        form.phone = data.phone
        form.portrait = data.portrait
        form.selfIntroduction = data.selfIntroduction
        form.sign = data.sign
        form.site = data.site
        form.wxYard = data.wxYard
        //找到职位在的位置
        form.post = data.post
        let postIndex = that.data.postArr.findIndex(item => item == data.post)
        if (postIndex == -1) {
          postIndex = 0
          form.post = that.data.postArr[postIndex]
        }
        // console.log(postIndex, that.data.postArr)
        this.setData({
          form,
          postIndex
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 微信二维码上传
   */
  async uploadWxYard () {
    const image = await ajax.chooseImage()
    const res = await ajax.uploadFile({ filePath: image })
    this.setData({ ['form.wxYard']: res.data.url })
  },

  /**
   * 删除微信二维码
   */
  deleteWxYard: function () {
    this.setData({
      ['form.wxYard']: ''
    })
  },

  /**
   *头像上传
   */
  async uploadPortrait () {
    const filePath = await ajax.chooseImage()
    const res = await ajax.uploadFile({ filePath })
    this.setData({ 'form.portrait': res.data.url })
  },

  /**
   * 删除头像
   */
  deletePortrait: function () {
    this.setData({
      ['form.portrait']: ''
    })
  },

  /**
   * 表单提交
   */
  submit: function (e) {
    let that = this
    console.log(e.detail.value)
    let formData = e.detail.value
    let form = this.data.form
    form.companyProfile = formData.companyProfile
    form.mailbox = formData.mailbox
    form.name = formData.name
    form.phone = formData.phone
    form.selfIntroduction = formData.selfIntroduction
    form.site = formData.site

    console.log(form)
    //表单拦截
    if (!form.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (!form.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (!form.companyProfile) {
      wx.showToast({
        title: '请输入企业介绍',
        icon: 'none'
      })
      return
    }
    if (!form.site) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return
    }
    if (!form.selfIntroduction) {
      wx.showToast({
        title: '请输入个人简介',
        icon: 'none'
      })
      return
    }
    if (!form.wxYard) {
      wx.showToast({
        title: '请上传微信二维码',
        icon: 'none'
      })
      return
    }
    if (!form.portrait) {
      wx.showToast({
        title: '请上传个人照片',
        icon: 'none'
      })
      return
    }

    ajax.post('/wx/appletCard/appletEdit', form).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '修改成功',
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_card_detail(options.cardId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})